# -*- coding: utf-8 -*-

from openerp import models, fields, api

class sale_quotation(models.Model):
    _name = 'sale.quotation'

    name=fields.Char(string=u"编号")
    write_time=fields.Date(string=u"日期",default=fields.Datetime.now())
    partner_id=fields.Many2one('res.partner',string=u"收件人")
    date_qty=fields.Date(string=u"有效期")
    user_id=fields.Many2one('res.users',string=u"业务员",default=lambda self: self.env.user,readonly=True)
    quotation_ids=fields.One2many('sale.quotation.line','sale_quotation_id',string=u"产品目录")
    state=fields.Selection([
        ('draft',u'草稿'),
        ('submit',u'市场组审核'),
        ('confirmed',u'销售总监审核'),
        ('done',u'审核通过')
        ],string=u"状态",default='draft')

    _defaults = {
        'name': lambda obj, cr, uid, context: '/',
    }

    _order = 'write_time desc, id desc'

    #权限所需要的字段
    user_manager=fields.Many2one('res.users',string=u"创建客户用户的部门经理",store=True)
    user_manager2=fields.Many2one('res.users',string=u"创建用户的上级部门经理",store=True)
    user_manager3=fields.Many2one('res.users',string=u"当前用户的上级部门副经理",store=True)

    @api.one
    def _get_manager_name(self):
        return self.user_manager.name

    @api.one
    def _get_manager2_name(self):
        return self.user_manager2.name

    @api.onchange('user_id')
    def _onchange_create_uid(self):
        if self.user_id and self.user_id.employee_ids[0]:
            self.user_manager = self.user_id.employee_ids[0].department_id.manager_id.user_id
            if self.user_id.employee_ids[0].department_id.parent_id:
                self.user_manager2=self.user_id.employee_ids[0].department_id.parent_id.manager_id.user_id
                self.user_manager3 = self.user_id.employee_ids[0].department_id.parent_id.fumanager_id.user_id



    def create(self, cr, uid, vals, context=None):
        if context is None:
            context = {}
        if vals.get('name', '/') == '/':
            vals['name'] = self.pool.get('ir.sequence').get(cr, uid, 'sale.quotation') or '/'
        new_id = super(sale_quotation, self).create(cr, uid, vals, context=context)
        return new_id

    @api.one
    def action_draft(self):
        self.write({'state':'draft'})

    @api.one
    def action_submit(self):
        self.write({'state':'submit'})
        self.write_time = fields.Datetime.now()

    @api.one
    def action_confirm(self):
        self.write({'state':'confirmed'})

    @api.one
    def action_done(self):
        self.write({'state':'done'})

    @api.one
    def action_cancel(self):
        self.write({'state':'draft'})


class sale_quotation_line(models.Model):
    _name='sale.quotation.line'

    sale_quotation_id=fields.Many2one('sale.quotation',ondelete='cascade',string=u"报价单")
    product_tpl_id = fields.Many2one(comodel_name='product.template',string=u'材料类型')
    product_name=fields.Many2one(comodel_name='product.attribute.value',string=u'材料名称',domain="[('id', 'in', possible_values_name[0][2])]")
    possible_values_name = fields.Many2many(comodel_name='product.attribute.value',compute='_get_possible_attribute_values_name')
    product_uom = fields.Many2one(comodel_name='product.uom', string=u'单位',related='product_tpl_id.uom_id')
    price=fields.Float(string=u"材料单价")
    use_qty=fields.Char(string=u"用量")

    @api.one
    @api.depends('product_tpl_id',
                 'product_tpl_id.attribute_line_ids')
    def _get_possible_attribute_values_name(self):
        print('_get_possible_attribute_values_name',self)
        attr_values = self.env['product.attribute.value']
        index = 0
        for attr_line in self.product_tpl_id.attribute_line_ids:
            if self.product_tpl_id.name==u'施工机具':
                if index==0:
                    attr_values |= attr_line.value_ids
            elif index == 1:
                attr_values |= attr_line.value_ids
            index +=1
        self.possible_values_name = attr_values.sorted()